import requests
import codecs
from bs4 import BeautifulSoup
import sys
import importlib
importlib.reload(sys)

headers={'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.99 Safari/537.36'}
server = 'http://www.biquge.com'
book = 'http://www.biquge.com/135_135874/'
global save_path
save_path = 'F:/渡劫之王'

# 获取章节内容
def get_contents(chapter):
    req = requests.get(url=chapter)
    html = req.content
    html_doc=str(html,'utf-8')
    bf = BeautifulSoup(html_doc, 'html.parser')
    text = bf.find_all('div',id='content')
    content=text[0].text.replace('\xa0' * 4, '\n')
    return content

# 写入文件
def write_txt(chapter,content,code):
    with codecs.open(chapter,'w',encoding=code) as f:
        f.write(content)
        
res=requests.get(book,headers=headers)
print(res.encoding)
html=res.content
html_doc=str(html,'utf-8')
# print(html_doc)
soup=BeautifulSoup(html_doc,'html.parser')
a=soup.find('div', id='list').find_all('a')
#print(a)
print('总章节数：%d' % len(a))
print(a[0].get_text())
chapter = server + a[0].get('href');
print(chapter)
content=get_contents(chapter)
print(content)
fullpath=save_path+'/'+a[0].get_text()+'.txt'
write_txt(fullpath,content,'utf-8')

# try:
         # chapter = server + each.get('href')
         # print(chapter)
         # content = get_contents(chapter)
         # chapter = save_path + '/' + each.string + '.txt'
         # write_txt(chapter,ocntent, 'utf-8')
     # except Exception as e:
         # print(e)
# for each in a:
     # try:
         # chapter = server + each.get('href')
         # print(chapter)
         # content = get_contents(chapter)
         # chapter = save_path + '/' + each.string + '.txt'
         # write_txt(chapter,ocntent, 'utf-8')
     # except Exception as e:
         # print(e)